#include "fileitem.h"
#include "ui_fileitem.h"
#include <QMessageBox>
#define UNIT_KB 1024            //KBD
#define UNIT_MB 1024 * 1024       //MB
#define UNIT_GB 1024 * 1024 * 1024  //GB

#define TIME_INTERVAL 300       //0.3s

FileItem::FileItem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FileItem)
    , m_lastBytes(0)
    , m_timer()
{
    m_timer.start(); // 启动计时器
    ui->setupUi(this);
    connect(ui->btnControl,SIGNAL(clicked()),this,SLOT(onControl()));
    connect(ui->btnCancel,SIGNAL(clicked()),this,SLOT(onCancel()));
}

FileItem::~FileItem()
{
    delete ui;
}

void FileItem::setFileName(const QString name)
{
    ui->fileNameLabel->setText(name);
}

void FileItem::setFileSize(const int size)
{
    QString sizeStr = transformUnit(size, false);
    ui->fileSizeLabel->setText(sizeStr);
}

void FileItem::setFilePath(const QString path)
{
    this->filePath = path;
}

QString FileItem::getFilePath()
{
    return this->filePath;
}

QListWidgetItem* FileItem::getListItem()
{
    return this->listItem;
}

QString FileItem::transformUnit(qint64 bytes, bool isSpeed)
{
    QString strUnit = " B";
    if (bytes <= 0)
    {
        bytes = 0;
    }
    else if (bytes < UNIT_KB)
    {
    }
    else if (bytes < UNIT_MB)
    {
        bytes /= UNIT_KB;
        strUnit = " KB";
    }
    else if (bytes < UNIT_GB)
    {
        bytes /= UNIT_MB;
        strUnit = " MB";
    }
    else if (bytes > UNIT_GB)
    {
        bytes /= UNIT_GB;
        strUnit = " GB";
    }

    if (isSpeed)
    {
        strUnit += "/S";
    }
    return QString("%1%2").arg(bytes).arg(strUnit);
}

QString FileItem::transformTime(qint64 seconds)
{
    QString strValue;
    QString strSpacing(" ");
    if (seconds <= 0)
    {
        strValue = QString("%1s").arg(0);
    }
    else if (seconds < 60)
    {
        strValue = QString("%1s").arg(seconds);
    }
    else if (seconds < 60 * 60)
    {
        int nMinute = seconds / 60;
        int nSecond = seconds - nMinute * 60;

        strValue = QString("%1m").arg(nMinute);

        if (nSecond > 0)
            strValue += strSpacing + QString("%1s").arg(nSecond);
    }
    else if (seconds < 60 * 60 * 24)
    {
        int nHour = seconds / (60 * 60);
        int nMinute = (seconds - nHour * 60 * 60) / 60;
        int nSecond = seconds - nHour * 60 * 60 - nMinute * 60;

        strValue = QString("%1h").arg(nHour);

        if (nMinute > 0)
            strValue += strSpacing + QString("%1m").arg(nMinute);

        if (nSecond > 0)
            strValue += strSpacing + QString("%1s").arg(nSecond);
    }
    else
    {
        int nDay = seconds / (60 * 60 * 24);
        int nHour = (seconds - nDay * 60 * 60 * 24) / (60 * 60);
        int nMinute = (seconds - nDay * 60 * 60 * 24 - nHour * 60 * 60) / 60;
        int nSecond = seconds - nDay * 60 * 60 * 24 - nHour * 60 * 60 - nMinute * 60;

        strValue = QString("%1d").arg(nDay);

        if (nHour > 0)
            strValue += strSpacing + QString("%1h").arg(nHour);

        if (nMinute > 0)
            strValue += strSpacing + QString("%1m").arg(nMinute);

        if (nSecond > 0)
            strValue += strSpacing + QString("%1s").arg(nSecond);
    }
    return strValue;
}

void FileItem::setSpeed(qint64 speed)
{
    QString speedStr = transformUnit(speed, true);
    ui->speedLabel->setText(speedStr);
}

void FileItem::setRemainingTime(qint64 seconds)
{
    QString timeStr = transformTime(seconds);
    ui->timeLabel->setText(timeStr);
}

void FileItem::onControl()
{

}

void FileItem::onCancel()
{

}

void FileItem::updateProgress(qint64 transferred, qint64 total)
{
    // 1. 实时更新进度条
    ui->progressBar->setMaximum(total);
    ui->progressBar->setValue(transferred);

    // 2. 时间间隔控制
    qint64 elapsed = m_timer.elapsed(); // 获取经过的毫秒数

    if (elapsed < m_updateInterval) {
        return; // 未达到更新间隔，跳过速度和时间计算
    }

    // 3. 计算下载速度
    qint64 deltaBytes = transferred - m_lastBytes;
    qint64 bytesPerSec = (deltaBytes * 1000) / elapsed; // 字节/秒

    // 4. 更新速度显示
    setSpeed(bytesPerSec);

    // 5. 计算剩余时间
    if (bytesPerSec > 0) {
        qint64 remainingBytes = total - transferred;
        qint64 remainingTime = remainingBytes / bytesPerSec;
        setRemainingTime(remainingTime);
    }

    // 6. 重置计时器和计数器
    m_timer.restart();       // 重置计时器
    m_lastBytes = transferred;  // 保存当前字节数
}



